package com.fengniao.goods.service.impl;

import com.fengniao.goods.dao.CategoryMapper;
import com.fengniao.goods.dao.ParaMapper;
import com.fengniao.goods.pojo.Category;
import com.fengniao.goods.pojo.Para;
import com.fengniao.goods.service.ParaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.DBOperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * @author lanhai
 * @date 2021/7/5 17:01
 */
@Service
public class ParaServiceImpl implements ParaService {

    @Autowired
    private ParaMapper paraMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 条件查询代码抽取
     * @param para
     * @return
     */
    public Example getExample(Para para) {
        Example example = new Example(Para.class);
        Example.Criteria criteria = example.createCriteria();

        if (para != null) {
            if (!StringUtils.isEmpty(para.getId())) {
                criteria.andEqualTo("id", para.getId());
            }
            if (!StringUtils.isEmpty(para.getOptions())) {
                criteria.andEqualTo("options", para.getOptions());
            }
            if (!StringUtils.isEmpty(para.getName())) {
                criteria.andLike("name", "%" + para.getName() + "%");
            }
            if (!StringUtils.isEmpty(para.getTemplateId())) {
                criteria.andEqualTo("template_id", para.getTemplateId());
            }
            if (!StringUtils.isEmpty(para.getSeq())) {
                criteria.andEqualTo("seq", para.getSeq());
            }

        }
        return example;
    }

    @Override
    public PageInfo<Para> findPage(Para para, Integer num, Integer size) {

        PageHelper.startPage(num, size);
        List<Para> Paras = findList(para);
        return new PageInfo<>(Paras);
    }

    @Override
    public PageInfo<Para> findPage(Integer num, Integer size) {

        PageHelper.startPage(num, size);
        List<Para> Paras = paraMapper.selectAll();
        return new PageInfo<>(Paras);
    }

    @Override
    public List<Para> findList(Para para) {

        Example example = getExample(para);
        return paraMapper.selectByExample(example);
    }

    @Override
    public List<Para> findAll() {
        return paraMapper.selectAll();
    }

    @Override
    public Para findById(Integer id) {
        return paraMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(Para para) {
        return paraMapper.insertSelective(para);
    }

    @Override
    public int revise(Para para) {
        return paraMapper.updateByPrimaryKeySelective(para);
    }

    @Override
    public int remove(Integer id) {
        return paraMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Para> findByCategoryId(Integer categoryId) {
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        Para para = new Para();
        para.setTemplateId(category.getTemplateId());
        List<Para> paras = paraMapper.select(para);
        DBOperUtil.ListIsNull(paras);
        return paras;
    }
}
